console.log('------------------------------------------216-------------------------------------------')
// 1.最大值，最小值，和，平均值
var arr = [1, 8, 9, 2, 4, 5, 5, 5, 44, 51, 54, 12, 45, 15, 15, 41, 15, 13, 15]

function test(arr) {
    if (!Array.isArray(arr)) return
    var max = arr[0]
    var min = arr[0]
    var sum = 0
    var avg = arr[0]
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i]
        } else {
            min = arr[i]
        }
        sum += arr[i]
    }
    avg = sum / arr.length
    console.log(max)
    console.log(min)
    console.log(sum)
    console.log(avg.toFixed(2))
}
test(arr)

// 2.分割
var names = ['郭德纲', '郭麒麟', '于谦', '岳云鹏', '烧饼'];
var Name1 = names.join('|')
console.log(Name1)
var Name2 = Name1.split('|')
console.log(Name2)
var Name3 = Name2.reverse()
console.log(Name3)

// 3.冒泡排序
var arr3 = [5, 9, 3, 8, 2, 6, 74, 2, 5, 6, 5, 45]
console.log(arr3)
for (var i = 0; i < arr3.length; i++) {
    for (var j = 0; j < arr3.length - i - 1; j++) {
        if (arr3[j] > arr3[j + 1]) {
            let tem = arr3[j]
            arr3[j] = arr3[j + 1]
            arr3[j + 1] = tem
        }
    }
}
console.log(arr3)

// 4.随机颜色
function random(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min)
}
function randomRGB(min, max) {
    var color1 = random(min, max)
    var color2 = random(min, max)
    var color3 = random(min, max)
    return 'rgb(' + color1 + ',' + color2 + ',' + color3 + ')'
}
console.log(randomRGB(126, 242))

// 5.封装日期函数
function dataList(data) {
    if (!(data instanceof Date)) return
    var year = data.getFullYear(),
        month = data.getMonth() + 1,
        day = data.getDate(),
        hour = data.getHours(),
        min = data.getMinutes(),
        s = data.getSeconds()

    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    hour = hour < 10 ? '0' + hour : hour
    min = month < 10 ? '0' + min : min
    s = month < 10 ? '0' + s : s
    return year + '-' + month + '-' + day + '|' + hour + ':' + min + ':' + s
}
var newDate = new Date()
console.log(dataList(newDate))

// 6.['c', 'a', 'z', 'a', 'x', 'a']
var arr6 = ['c', 'a', 'z', 'a', 'x', 'a']
var newarr6 = []
for (var i = 0; i < arr6.length; i++) {
    if (newarr6.indexOf(arr6[i]) == -1) {
        newarr6.push(arr6[i])
    }
}
console.log(newarr6)

// 7.['c', 'a', 'z', 'a', 'x', 'a']中每一个a出现的位置
var arr7 = ['c', 'a', 'z', 'a', 'x', 'a']
var index = -1
do {
    index = arr7.indexOf('a', index + 1)
    if (index !== -1) {
        console.log(index)
    }

} while (index !== -1)

// 8.截取字符串""，中的"中华"
var str8 = '我爱中华人民共和国'
var newstr8 = str8.substr(2, 2)
console.log(newstr8)

// 9.'abcoefoxyozzopp',o的位置

var str9 = 'abcoefoxyozzopp'
var index9 = -1
do {
    index9 = str9.indexOf('o', index9 + 1)
    if (index9 !== -1) {
        console.log(index9)
    }

} while (index9 !== -1)

// 10.'abcoefoxyozzopp',o的替换

var str10 = 'abcoefoxyozzopp'
var index10 = -1
do {
    index10 = str10.indexOf('o', index10 + 1)
    if (index10 !== -1) {
        str10 = str10.replace('o', '!')
    }

} while (index10 !== -1)

console.log(str10)

// 11.去空白
var s = '   abc       xyz  a    123   ';
var arr11 = s.split(' ')
var news = arr11.join('')
console.log(news)

// 12.判断字符串出现的次数
var s12 = 'abcoefoxyozzopp';
var obj = {}
var sum = 0
var ch
for (var i = 0; i < s12.length; i++){
    var item = s12.charAt(i)
    if (obj[item]) {
        obj[item]++
    } else {
       obj[item] = 1
    }
}
console.log(obj)
for (key in obj) {
    if (obj[key] > sum) {
        sum = obj[key]
        ch = key
    }
}
console.log(sum)
console.log(ch)

// 13.获取url后面的内容
var url = 'http://www.itheima.com/login?name=zs&age=18&a=1&b=2';
function getparms(url) {
    var index = url.indexOf('?') + 1
    var parms = url.substr(index)
    var arr = parms.split('&')
    var obj = {}
    for (var i = 0; i < arr.length; i++){
        var tem = arr[i].split('=')
        var key = tem[0]
        var value = tem[1]
        obj[key] = value
    }
    return obj
}
console.log(getparms(url))


